package com.yswy.backstage.scheduled;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class Testcj {
    static  Integer cc = 0;
    public static void main(String[] args) {
        List<String> stringList = getFileContext("/Users/mac/Desktop/test.txt" );
//        for (int i = 0; i < stringList.size(); i++) {
//            String token = stringList.get(i);
//            new Thread(new Runnable() {
//                public void run() {
//                    while (true){
//                        try {
//                            int a=(int)(Math.random()*(3))+1;
//                            Integer ss = 1 ;
//                            if (a == 1){
//                                ss = 5 ;
//                            }else if (a == 2){
//                                ss = 10 ;
//                            }
//                            cc+=ss ;
//                            log.info(cc+"");
//                            if (cc>=(200)){
//                                return;
//                            }
//                            sendGetRequest(ss+"" ,
//                                    token);
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            }).start();
//        }

        for (int i = 0; i < stringList.size(); i++) {
            String token = stringList.get(i);
            System.out.println(token);
            new Thread(new Runnable() {
                public void run() {
                    for (int j = 0; j < 1000; j++) {
                        try {
                            //int a=(int)(Math.random()*(3))+1;
                            Integer ss = 1 ;
//                            if (a == 1){
//                                ss = 5 ;
//                            }else if (a == 2){
//                                ss = 10 ;
//                            }
                            Thread.sleep(2000);
                            long startTime =  System.currentTimeMillis();

                            sendGetRequest(ss+"" ,
                                    token);
                            long endTime =  System.currentTimeMillis();

                            long usedTime = (endTime-startTime);
                            log.info(usedTime+"");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        }

    }

    public static void sendGetRequest(String urls ,String token) throws MalformedURLException, IOException {
        // Make a URL to the web page
        //URL url = new URL("http://127.0.0.1:9111/app/raffle/drawRaffle?groupId=1&raffleNum="+urls);
        URL url = new URL("http://121.36.50.201:9111/app/raffle/drawRaffle?groupId=1&raffleNum="+urls);

        //URL url = new URL("http://121.36.50.201:9111/app/raffle/test");
        // Get the input stream through URL Connection
        URLConnection con = url.openConnection();

        //add request header
        con.setRequestProperty("Authorization", token);

        InputStream is = con.getInputStream();

        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        String line = null;

        // read each line and write to System.out
        while ((line = br.readLine()) != null) {
            //log.info(line);
        }
        br.close();
        is.close();

    }


    /**
        * 获取txt文件内容, 并按行放入list中
        */
    public static List<String> getFileContext(String path ) {
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        List<String> list = new ArrayList<String>();
        String str = "";
        try {
        fileReader = new FileReader( path );
      bufferedReader = new BufferedReader( fileReader );
      while( (str = bufferedReader.readLine()) != null ) {
        if( str.trim().length() > 2 ) {
          list.add( str );
        }
      }
    } catch ( Exception e ) {
      e.printStackTrace();
    } finally {
      try {
        if (bufferedReader != null) {
          bufferedReader.close();
        }
      } catch (Exception e2) {
        e2.printStackTrace();
      }

      try {
        if (fileReader != null) {
          fileReader.close();
        }
      } catch (Exception e2) {
        e2.printStackTrace();
      }
    }
    return list;
  }
}
